<template><div><h2 id="二分查找" tabindex="-1"><a class="header-anchor" href="#二分查找"><span>二分查找</span></a></h2>
<div class="language-c++ line-numbers-mode has-collapsed-lines collapsed" data-highlighter="shiki" data-ext="c++" style="--vp-collapsed-lines:15;background-color:#282c34;color:#abb2bf"><pre class="shiki one-dark-pro vp-code" v-pre=""><code><span class="line"><span style="color:#C678DD">#include</span><span style="color:#98C379">&#x3C;iostream></span></span>
<span class="line"><span style="color:#C678DD">using</span><span style="color:#C678DD"> namespace</span><span style="color:#E5C07B"> std</span><span style="color:#ABB2BF">;</span></span>
<span class="line"><span style="color:#C678DD">const</span><span style="color:#C678DD"> int</span><span style="color:#ABB2BF"> N </span><span style="color:#C678DD">=</span><span style="color:#D19A66"> 1</span><span style="color:#E06C75">e</span><span style="color:#D19A66">5</span><span style="color:#C678DD">+</span><span style="color:#D19A66">10</span><span style="color:#ABB2BF">; </span></span>
<span class="line"><span style="color:#C678DD">int</span><span style="color:#E5C07B"> arr</span><span style="color:#ABB2BF">[N];</span></span>
<span class="line"><span style="color:#C678DD">int</span><span style="color:#61AFEF"> main</span><span style="color:#ABB2BF">(){</span></span>
<span class="line"><span style="color:#C678DD">    int</span><span style="color:#ABB2BF"> n;cin</span><span style="color:#C678DD">>></span><span style="color:#ABB2BF">n;</span></span>
<span class="line"><span style="color:#C678DD">    for</span><span style="color:#ABB2BF">(</span><span style="color:#C678DD">int</span><span style="color:#ABB2BF"> i</span><span style="color:#C678DD">=</span><span style="color:#D19A66">0</span><span style="color:#ABB2BF">;i</span><span style="color:#C678DD">&#x3C;</span><span style="color:#ABB2BF">n;i</span><span style="color:#C678DD">++</span><span style="color:#ABB2BF">) cin</span><span style="color:#C678DD">>></span><span style="color:#E5C07B">arr</span><span style="color:#ABB2BF">[i];</span></span>
<span class="line"><span style="color:#C678DD">    int</span><span style="color:#ABB2BF"> ser;cin</span><span style="color:#C678DD">>></span><span style="color:#ABB2BF">ser;</span></span>
<span class="line"><span style="color:#C678DD">    int</span><span style="color:#ABB2BF"> l </span><span style="color:#C678DD">=</span><span style="color:#D19A66"> 0</span><span style="color:#ABB2BF">; </span><span style="color:#C678DD">int</span><span style="color:#ABB2BF"> r </span><span style="color:#C678DD">=</span><span style="color:#ABB2BF"> n</span><span style="color:#C678DD">-</span><span style="color:#D19A66">1</span><span style="color:#ABB2BF">;</span></span>
<span class="line"><span style="color:#C678DD">    while</span><span style="color:#ABB2BF">(l</span><span style="color:#C678DD">&#x3C;=</span><span style="color:#ABB2BF">r){</span></span>
<span class="line"><span style="color:#C678DD">        int</span><span style="color:#ABB2BF"> mid </span><span style="color:#C678DD">=</span><span style="color:#ABB2BF"> l</span><span style="color:#C678DD">+</span><span style="color:#ABB2BF">(r</span><span style="color:#C678DD">-</span><span style="color:#ABB2BF">l)</span><span style="color:#C678DD">/</span><span style="color:#D19A66">2</span><span style="color:#ABB2BF">;</span></span>
<span class="line"><span style="color:#C678DD">        if</span><span style="color:#ABB2BF">(</span><span style="color:#E5C07B">arr</span><span style="color:#ABB2BF">[mid] </span><span style="color:#C678DD">==</span><span style="color:#ABB2BF"> ser){</span></span>
<span class="line"><span style="color:#ABB2BF">            cout</span><span style="color:#C678DD">&#x3C;&#x3C;</span><span style="color:#ABB2BF">mid</span><span style="color:#C678DD">&#x3C;&#x3C;</span><span style="color:#ABB2BF">endl;</span></span>
<span class="line"><span style="color:#C678DD">            return</span><span style="color:#D19A66"> 0</span><span style="color:#ABB2BF">;</span></span>
<span class="line"><span style="color:#ABB2BF">        } </span></span>
<span class="line"><span style="color:#C678DD">        else</span><span style="color:#C678DD"> if</span><span style="color:#ABB2BF">(</span><span style="color:#E5C07B">arr</span><span style="color:#ABB2BF">[mid] </span><span style="color:#C678DD">&#x3C;</span><span style="color:#ABB2BF"> ser) l </span><span style="color:#C678DD">=</span><span style="color:#ABB2BF"> mid</span><span style="color:#C678DD">+</span><span style="color:#D19A66">1</span><span style="color:#ABB2BF">;</span></span>
<span class="line"><span style="color:#C678DD">        else</span><span style="color:#C678DD"> if</span><span style="color:#ABB2BF">(</span><span style="color:#E5C07B">arr</span><span style="color:#ABB2BF">[mid] </span><span style="color:#C678DD">></span><span style="color:#ABB2BF"> ser) r </span><span style="color:#C678DD">=</span><span style="color:#ABB2BF"> mid</span><span style="color:#C678DD">-</span><span style="color:#D19A66">1</span><span style="color:#ABB2BF">;</span></span>
<span class="line"><span style="color:#ABB2BF">    }</span></span>
<span class="line"><span style="color:#ABB2BF">    cout</span><span style="color:#C678DD">&#x3C;&#x3C;</span><span style="color:#98C379">"-1"</span><span style="color:#C678DD">&#x3C;&#x3C;</span><span style="color:#ABB2BF">endl;</span></span>
<span class="line"><span style="color:#C678DD">    return</span><span style="color:#D19A66"> 0</span><span style="color:#ABB2BF">;</span></span>
<span class="line"><span style="color:#ABB2BF">}</span></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div><div class="collapsed-lines"></div></div><h2 id="移除元素" tabindex="-1"><a class="header-anchor" href="#移除元素"><span>移除元素</span></a></h2>
<div class="language-c++ line-numbers-mode" data-highlighter="shiki" data-ext="c++" style="background-color:#282c34;color:#abb2bf"><pre class="shiki one-dark-pro vp-code" v-pre=""><code><span class="line"><span style="color:#C678DD">#include</span><span style="color:#98C379">&#x3C;iostream></span></span>
<span class="line"><span style="color:#C678DD">using</span><span style="color:#C678DD"> namespace</span><span style="color:#E5C07B"> std</span><span style="color:#ABB2BF">;</span></span>
<span class="line"><span style="color:#C678DD">const</span><span style="color:#C678DD"> int</span><span style="color:#ABB2BF"> N </span><span style="color:#C678DD">=</span><span style="color:#D19A66"> 1</span><span style="color:#E06C75">e</span><span style="color:#D19A66">5</span><span style="color:#C678DD">+</span><span style="color:#D19A66">10</span><span style="color:#ABB2BF">;</span></span>
<span class="line"><span style="color:#C678DD">int</span><span style="color:#E5C07B"> arr</span><span style="color:#ABB2BF">[N];</span></span>
<span class="line"><span style="color:#C678DD">int</span><span style="color:#61AFEF"> main</span><span style="color:#ABB2BF">(){</span></span>
<span class="line"><span style="color:#C678DD">    int</span><span style="color:#ABB2BF"> n;cin</span><span style="color:#C678DD">>></span><span style="color:#ABB2BF">n;</span></span>
<span class="line"><span style="color:#C678DD">    for</span><span style="color:#ABB2BF">(</span><span style="color:#C678DD">int</span><span style="color:#ABB2BF"> i </span><span style="color:#C678DD">=</span><span style="color:#D19A66"> 0</span><span style="color:#ABB2BF">;i</span><span style="color:#C678DD">&#x3C;</span><span style="color:#ABB2BF">n;i</span><span style="color:#C678DD">++</span><span style="color:#ABB2BF">) cin</span><span style="color:#C678DD">>></span><span style="color:#E5C07B">arr</span><span style="color:#ABB2BF">[i];</span></span>
<span class="line"><span style="color:#C678DD">    int</span><span style="color:#ABB2BF"> del;cin</span><span style="color:#C678DD">>></span><span style="color:#ABB2BF">del;</span></span>
<span class="line"><span style="color:#C678DD">    int</span><span style="color:#ABB2BF"> slow </span><span style="color:#C678DD">=</span><span style="color:#D19A66"> 0</span><span style="color:#ABB2BF">;</span></span>
<span class="line"><span style="color:#C678DD">    for</span><span style="color:#ABB2BF">(</span><span style="color:#C678DD">int</span><span style="color:#ABB2BF"> fast </span><span style="color:#C678DD">=</span><span style="color:#D19A66"> 0</span><span style="color:#ABB2BF">;fast </span><span style="color:#C678DD">&#x3C;</span><span style="color:#ABB2BF"> n;fast</span><span style="color:#C678DD">++</span><span style="color:#ABB2BF">) </span><span style="color:#C678DD">if</span><span style="color:#ABB2BF">(</span><span style="color:#E5C07B">arr</span><span style="color:#ABB2BF">[fast] </span><span style="color:#C678DD">!=</span><span style="color:#ABB2BF"> del) </span><span style="color:#E5C07B">arr</span><span style="color:#ABB2BF">[slow</span><span style="color:#C678DD">++</span><span style="color:#ABB2BF">] </span><span style="color:#C678DD">=</span><span style="color:#E5C07B"> arr</span><span style="color:#ABB2BF">[fast];</span><span style="color:#7F848E;font-style:italic"> // 快慢指针</span></span>
<span class="line"><span style="color:#C678DD">    for</span><span style="color:#ABB2BF">(</span><span style="color:#C678DD">int</span><span style="color:#ABB2BF"> i </span><span style="color:#C678DD">=</span><span style="color:#D19A66"> 0</span><span style="color:#ABB2BF">;i </span><span style="color:#C678DD">&#x3C;</span><span style="color:#ABB2BF"> n;i</span><span style="color:#C678DD">++</span><span style="color:#ABB2BF">) cout</span><span style="color:#C678DD">&#x3C;&#x3C;</span><span style="color:#E5C07B">arr</span><span style="color:#ABB2BF">[i]</span><span style="color:#C678DD">&#x3C;&#x3C;</span><span style="color:#98C379">" "</span><span style="color:#ABB2BF">;</span></span>
<span class="line"><span style="color:#C678DD">    return</span><span style="color:#D19A66"> 0</span><span style="color:#ABB2BF">;</span></span>
<span class="line"><span style="color:#ABB2BF">}</span></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div></div></template>


